/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.delivery.api.controller;

import com.google.gson.Gson;
import com.yami.shop.bean.app.dto.DeliveryDto;
import com.yami.shop.bean.model.Delivery;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.delivery.comment.model.DeliveryOrder;
import com.yami.shop.delivery.comment.service.DeliveryOrderService;
import com.yami.shop.service.DeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/p/myDelivery")
@Api(tags = "我的物流接口")
@AllArgsConstructor
public class MyDeliveryController {


    @Autowired
    private final DeliveryOrderService deliveryOrderService;
    @Autowired
    private final DeliveryService deliveryService;

    /**
     * 物流查询接口
     */
    @GetMapping("/orderInfo/{orderNumber}")
    @ApiOperation(value = "物流查询接口", notes = "根据orderNumber查询订单所有包裹信息")
    public ResponseEntity<List<DeliveryOrder>> info(@PathVariable("orderNumber") String orderNumber) {
        //查询包裹信息
        List<DeliveryOrder> deliveryOrders = deliveryOrderService.getAndDeliveryItemInfo(orderNumber);
        // 查询物流详情信息
        // 物流公司名称、官网、订单号、物流详情信息
        try {
            for (DeliveryOrder deliveryOrder : deliveryOrders) {
                DeliveryDto deliveryDto;
                // 查询交易单
                Delivery delivery = deliveryService.getById(deliveryOrder.getDvyId());
                // 解析的物流详情明细
                String detail = deliveryService.query(delivery.getDvyId(), deliveryOrder.getDvyFlowId());
                deliveryDto = new Gson().fromJson(detail, DeliveryDto.class);
                deliveryDto.setCompanyHomeUrl(delivery.getCompanyHomeUrl());
                deliveryDto.setCompanyName(delivery.getDvyName());
                deliveryDto.setDvyFlowId(deliveryOrder.getDvyFlowId());
                deliveryOrder.setDeliveryDto(deliveryDto);
                if(Objects.nonNull(deliveryOrder.getDeliveryDto()) && CollectionUtils.isNotEmpty(deliveryOrder.getDeliveryDto().getTraces())){
                    Collections.reverse(deliveryOrder.getDeliveryDto().getTraces());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 查询出错
            throw new YamiShopBindException("yami.query.error");
        }
        return ResponseEntity.ok(deliveryOrders);
    }


}
